package com.maple.common.http;

import java.io.Serializable;

import io.swagger.annotations.ApiModel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@ApiModel(value = "Resp", description = "公共返回实体")
public class Resp<T> implements Serializable {

    /**
     * 自定义返回结构代码状态
     */
    public enum CodeStatus {
        /**
         * 返回码状态
         */
        SUCCESS(200), SERVER_FAIL(500);

        private final int code;

        CodeStatus(int code) {
            this.code = code;
        }

        public int getCode() {
            return code;
        }
    }

    /**
     * 状态码：200：成功，500：失败
     */
    private int code = CodeStatus.SUCCESS.code;

    /**
     * 失败原因
     */
    private String msg;

    /**
     * 成功返回的数据
     */
    private T data;

    public static <T> Resp<T> success() {
        return success(null, null);
    }

    public static <T> Resp<T> success(T data) {
        return success(null, data);
    }

    public static <T> Resp<T> success(String msg, T data) {
        return new Resp<T>().setCode(CodeStatus.SUCCESS.code).setMsg(msg).setData(data);
    }

    public static <T> Resp<T> fail(String msg) {
        return fail(msg, null);
    }

    public static <T> Resp<T> fail(String msg, T data) {
        return new Resp<T>().setCode(CodeStatus.SERVER_FAIL.code).setMsg(msg).setData(data);
    }

}
